package org.jground.zk.zulX;

import org.jground.util.ArrayUtils;
import org.jground.zk.util.ZKUtils;
import org.zkoss.zhtml.Table;
import org.zkoss.zhtml.Td;
import org.zkoss.zhtml.Tr;
import org.zkoss.zk.ui.Component;

public class TableX extends Table {

    private String[] tdStyles;
    private int nrOfColumns;

    public enum UITypeX {
        LABEL_CONTROL
    };

    public TableX() {

    }

    public TableX(int nrOfColumns, String... tdStyles) {
        this.nrOfColumns = nrOfColumns;
        this.tdStyles = tdStyles;
    }

    public TableX(String... tdStyles) {
        this(tdStyles.length, tdStyles);
    }

    public void addRow(Component... components) {
        Tr tr = new Tr();
        tr.setParent(this);

        for (int i = 0; i < components.length; i++) {
            Component component = components[i];
            Td td = new Td();
            td.setWidgetAttribute("valign", "top");

            if (i == components.length - 1) {
                int colspan = nrOfColumns - i;
                td.setAttribute("colspan", colspan);
            }
            String tdStyle = ArrayUtils.get(tdStyles, i);
            if (tdStyle != null) {
                td.setStyle(tdStyle);
            }
            td.setParent(tr);
            component.setParent(td);
        }
    }
    

    public void removeAll() {
        ZKUtils.removeAllChildren(this);
    }
}
